<?php
namespace app\qihan\model;

use think\Model;
use think\Loader;

class QihanCategory extends Model
{
    public function getContentAttr($value)
    {
        return htmlspecialchars_decode($value);
    }
    //保存
    public function storage($data = [])
    {
        if (!$data) {
            $data = request()->post();
        }
        // 验证
        if(!isset($data['name']) || !$data['name'] || strlen($data['name']) >50 ){
            $this->error = "分类名称不能为空，且长度不能超过50个字符";
            return false;
        }
        //保存
        if (isset($data['id']) && !empty($data['id'])) {
            //其他验证
            $map = array("name"=>$data["name"],"id" =>array("neq",$data['id']) );
            $find = $this->where($map)->find();
            if($find){
                $this->error = '保存失败，该分类已经存在';
                return false;
            }else{
                $res = $this->update($data);
            }
        } else {
            //其他验证
            $map = array("name"=>$data["name"]);
            $find = $this->where($map)->find();
            if($find){
                $this->error = '保存失败，该分类名称已经存在';
                return false;
            }else{
                $res = $this->create($data);
            }
        }
        if (!$res) {
            $this->error = '保存失败';
            return false;
        }
        return $res;
    }
}